/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package APIs;

import bllobjects.Account;
import dalfactory.SDALFactory;
import fsktms.request.RequestParameters;
import fsktms.request.RequestRespond;
import fsktms.request.api.RequestAPIGroup;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import utils.DataRow;
import utils.DataTable;

/**
 *
 * @author Mrkupi
 */
public class QuerryAPIs extends RequestAPIGroup
{
    public QuerryAPIs()
    {
        super("Querry");
    }
    
    public RequestRespond getUsers(RequestParameters params)
    {
        RequestRespond result = new RequestRespond();
        try {
            String name = params.getString("Name");
            
            DataTable dtAcc     =   SDALFactory.getInstance().getQuerry().getAccountByName( name );
            DataTable dtAccType =   SDALFactory.getInstance().getQuerry().getAllAccountType();
            ArrayList<Account> users  = new ArrayList();
            
            if( dtAcc.size() > 0 && dtAccType.size() > 0 )
            {
                for( int i = 0; i < dtAcc.size(); ++i)
                {
                    String description = "";
                    boolean isFind = false;
                    DataRow row = dtAcc.get(0);
                    for( int j = 0; !isFind && j < dtAccType.size(); ++j)
                    {
                        DataRow rowType = dtAccType.get( j );
                        if( rowType.get( "TypeID" ) == null ? row.get( "TypeID" ) == null : rowType.get( "TypeID" ).equals(row.get( "TypeID" )) )
                        {
                            description =   rowType.get("Description");
                            isFind      =   true;
                        }
                    }
                    if(dtAcc.size() > 0 && !("".equals(description)) )
                    {
                        users.add(new Account(row.get("UserID"),
                                row.get("UserName"),
                                Integer.parseInt(row.get("Experience")),
                                Integer.parseInt(row.get("Level")),
                                description));
                    }
                }
            }
            
            result.set(RequestParameters.KEY_RESULT, users);
        } catch (RemoteException ex) {
            Logger.getLogger(QuerryAPIs.class.getName()).log(Level.SEVERE, null, ex);
            result.setFailure(RequestParameters.VALUE_FAILURE_CODE_SERVER_ERROR, ex.getMessage());
        }
        
        return result;
    }
    
    public RequestRespond searchUsers(RequestParameters params)
    {
        RequestRespond result = new RequestRespond();
        try {
            String senderID = params.getString("SenderID");
            String searchString = params.getString("SearchString");
            
            DataTable dtAcc     =   SDALFactory.getInstance().getQuerry()
                    .getListAccountContains(senderID, searchString);
            ArrayList<Account> users  = new ArrayList();
            ArrayList<Boolean> isFriend = new ArrayList();
            
            for( int i = 0; i < dtAcc.size(); ++i)
            {
                DataRow row = dtAcc.get(i);
                users.add(new Account(row.get("UserID"), row.get("UserName"),
                            Integer.parseInt(row.get("Experience")),
                            Integer.parseInt(row.get("Level")),
                            row.get("TypeID")));
                isFriend.add(Boolean.parseBoolean(row.get("IsFriend")));
            }
            
            result.setResult(users);
            result.set("IsFriend", isFriend);
        } catch (RemoteException ex) {
            Logger.getLogger(QuerryAPIs.class.getName()).log(Level.SEVERE, null, ex);
            result.setFailure(RequestParameters.VALUE_FAILURE_CODE_SERVER_ERROR, ex.getMessage());
        }
        
        return result;
    }
}
